/**********************************************************************************************************
** 驱动文件：	jmkt-SDAxxx.c
** 驱动类型：	jmkt
** 设备名称：	精密空调
** 设备型号：	SDAxxx
** 设备品牌：	依米康
** 设备协议：	Modbus-RTU
** 驱动描述：	适用于SDA系列小型机房专用空调，协议为Modbus-RTU。
** 生成日期：	2019-02-19 21:13:54
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var returnAirTemperature;					//回风温度
	Var returnAirHumidity;						//回风湿度
	Var setTemperature;							//设定温度
	Var setHumidity;							//设定湿度
	Var onoff;									//开关机状态
	Var fanStatus;								//风机状态
	Var coolingStatus;							//制冷状态
	Var heatingStatus;							//加热状态
	Var humidificationStatus;					//加湿状态
	Var dehumidificationStatus;					//除湿状态
	Var compressor1PressureAlarm;				//压缩机1高/低压报警
	Var compressor2PressureAlarm;				//压缩机2高/低压报警
	Var temperatureAlarm;						//高/低温告警
	Var humidityAlarm;							//高/低湿告警
	Var airflowLossAlarm;						//气流丢失
	Var fanOverload;							//风机过载
	Var heaterOverload;							//加热器过载
	Var airFilterAlarm;							//空气过滤网告警
	Var returnAirTemperatureSensorFailure;		//回风温度探头故障
	Var supplyAirTemperatureSensorFailure;		//送风温度探头故障
	Var returnAirHumiditySensorFailure;			//回风湿度探头故障
	Var outdoorTemperatureSensorFailure;		//室外温度探头故障
	Var supplyAirTemperatureAlarm;				//送风温度警告
	Var humidifierOverload;						//加湿器电流过大
	Var humidifierLackOfWater;					//加湿器缺水
	Var noHumidifyingCurrent;					//无加湿电流
	Var overflowAlarm;							//溢流告警
	Var userAlarm;								//用户告警
	Var smokeAlarm;								//烟雾告警
	Var waterFlowSwitchAlarm;					//水流开关告警
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		FLOAT,
		0,
		READONLY,
		"returnAirTemperature",
		"回风温度",
		"℃",
		"回风温度",
		0,
		0,
		"回风温度过高",
		"回风温度过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"returnAirHumidity",
		"回风湿度",
		"%",
		"回风湿度",
		0,
		0,
		"回风湿度过高",
		"回风湿度过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"setTemperature",
		"设定温度",
		"℃",
		"空调设定温度值",
		0,
		0,
		"",
		"",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"setHumidity",
		"设定湿度",
		"%",
		"空调设定湿度值",
		0,
		0,
		"",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"onoff",
		"开关机状态",
		"",
		"空调开关机状态：0-关机；1-开机",
		0,
		0,
		"空调关机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"fanStatus",
		"风机状态",
		"",
		"风机状态：0-停止；1-运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"coolingStatus",
		"制冷状态",
		"",
		"制冷状态：0-停止；1-运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"heatingStatus",
		"加热状态",
		"",
		"加热状态：0-停止；1-运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"humidificationStatus",
		"加湿状态",
		"",
		"加湿状态：0-停止；1-运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"dehumidificationStatus",
		"除湿状态",
		"",
		"除湿状态：0-停止；1-运行",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"compressor1PressureAlarm",
		"压缩机1高/低压报警",
		"",
		"压缩机高压、低压告警：0-无报警；1-高压报警；2-低压报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"compressor2PressureAlarm",
		"压缩机2高/低压报警",
		"",
		"压缩机2高压、低压告警：0-无报警；1-高压报警；2-低压报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"temperatureAlarm",
		"高/低温告警",
		"",
		"高温、低温告警：0-无报警；1-高温报警；2-低温报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"humidityAlarm",
		"高/低湿告警",
		"",
		"高湿、低湿告警：0-无报警；1-髙湿报警；2-低湿报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"airflowLossAlarm",
		"气流丢失",
		"",
		"气流丢失：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"fanOverload",
		"风机过载",
		"",
		"风机过载：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"heaterOverload",
		"加热器过载",
		"",
		"加热器过载：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"airFilterAlarm",
		"空气过滤网告警",
		"",
		"空气过滤网告警：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"returnAirTemperatureSensorFailure",
		"回风温度探头故障",
		"",
		"回风温度探头故障：0-无故障；1-有故障",
		0,
		0,
		"有故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"supplyAirTemperatureSensorFailure",
		"送风温度探头故障",
		"",
		"送风温度探头故障：0-无故障；1-有故障",
		0,
		0,
		"有故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"returnAirHumiditySensorFailure",
		"回风湿度探头故障",
		"",
		"回风湿度探头故障：0-无故障；1-有故障",
		0,
		0,
		"有故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"outdoorTemperatureSensorFailure",
		"室外温度探头故障",
		"",
		"室外温度探头故障：0-无故障；1-有故障",
		0,
		0,
		"有故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"supplyAirTemperatureAlarm",
		"送风温度警告",
		"",
		"送风温度警告：0-无告警；1-有告警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"humidifierOverload",
		"加湿器电流过大",
		"",
		"加湿器电流过大：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"humidifierLackOfWater",
		"加湿器缺水",
		"",
		"加湿器缺水：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"noHumidifyingCurrent",
		"无加湿电流",
		"",
		"无加湿电流：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"overflowAlarm",
		"溢流告警",
		"",
		"溢流告警：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"userAlarm",
		"用户告警",
		"",
		"用户告警：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"smokeAlarm",
		"烟雾告警",
		"",
		"烟雾告警：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"waterFlowSwitchAlarm",
		"水流开关告警",
		"",
		"水流开关告警：0-无报警；1-有报警",
		0,
		0,
		"有报警",
		"",
		0.000,
		0.000
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			cmd[0] = addr;
			cmd[1] = 0x03;
			cmd[2] = 0x00;								
			cmd[3] = 0x00;
			cmd[4] = 0x00;	
			cmd[5] = 0x08;
			*len = CRC16_Padding(cmd, 6);
			break;
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	union{
		short 			dat16;
      	unsigned char  	dat8[2];
    }DAT16_8;						   //数据类型转换	
	Data_Struct 	*d = (Data_Struct*)data;
	
	if(buf!=NULL && buf[0]==addr && len>0 )
	{	
		if(CRC16_Check(buf,len))
		{	
			DAT16_8.dat8[1] = buf[5];
			DAT16_8.dat8[0] = buf[6];
			d->setTemperature.value.fnum = DAT16_8.dat16*0.1;
			DAT16_8.dat8[1] = buf[7];
			DAT16_8.dat8[0] = buf[8];
			d->setHumidity.value.fnum = DAT16_8.dat16*1.0;
			DAT16_8.dat8[1] = buf[11];
			DAT16_8.dat8[0] = buf[12];
			d->returnAirTemperature.value.fnum = DAT16_8.dat16*0.1;			
			DAT16_8.dat8[1] = buf[13];
			DAT16_8.dat8[0] = buf[14];
			d->returnAirHumidity.value.fnum = DAT16_8.dat16*1.0;					

			d->dehumidificationStatus.value.str 	  = buf[10]&0x20?"运行":"停止";
			Set_Var_Status(&d->dehumidificationStatus,	buf[10]&0x20?ON:OFF, NULL);	
			d->humidificationStatus.value.str		  = buf[10]&0x10?"运行":"停止";
			Set_Var_Status(&d->humidificationStatus,	buf[10]&0x10?ON:OFF, NULL);	
			d->coolingStatus.value.str				  = buf[10]&0x08?"运行":"停止";
			Set_Var_Status(&d->coolingStatus,			buf[10]&0x08?ON:OFF, NULL);	
			d->heatingStatus.value.str				  = buf[10]&0x04?"运行":"停止";
			Set_Var_Status(&d->heatingStatus,			buf[10]&0x04?ON:OFF, NULL);	
			d->fanStatus.value.str					  = buf[10]&0x02?"运行":"停止";
			Set_Var_Status(&d->fanStatus,				buf[10]&0x02?ON:OFF, NULL);	
			d->onoff.value.str						  = buf[10]&0x01?"开机":"关机";
			Set_Var_Status(&d->onoff,					buf[10]&0x01?Normal:Alarm, NULL);

			if(buf[15]&0x80){
				d->compressor1PressureAlarm.value.str	= "高压告警";
				Set_Var_Status(&d->compressor1PressureAlarm, Alarm, "高压告警");
			}
			else if(buf[15]&0x40){
				d->compressor1PressureAlarm.value.str	= "低压告警";
				Set_Var_Status(&d->compressor1PressureAlarm, Alarm, "低压告警");						
			}
			else{
				d->compressor1PressureAlarm.value.str	= "无告警";
				Set_Var_Status(&d->compressor1PressureAlarm, Normal, "无告警");
			}

			d->airflowLossAlarm.value.str		= buf[15]&0x20?"有告警":"无告警";
			Set_Var_Status(&d->airflowLossAlarm,  buf[15]&0x20?Alarm:Normal, NULL);			
			d->fanOverload.value.str			= buf[15]&0x10?"有告警":"无告警";
			Set_Var_Status(&d->fanOverload, 	  buf[15]&0x10?Alarm:Normal, NULL);		
			d->heaterOverload.value.str			= buf[15]&0x08?"有告警":"无告警";
			Set_Var_Status(&d->heaterOverload, 	  buf[15]&0x08?Alarm:Normal, NULL);				
			d->airFilterAlarm.value.str			= buf[15]&0x04?"有告警":"无告警";
			Set_Var_Status(&d->airFilterAlarm, 	  buf[15]&0x04?Alarm:Normal, NULL);	

			if(buf[15]&0x02){
				d->temperatureAlarm.value.str	= "高温告警";
				Set_Var_Status(&d->temperatureAlarm, Alarm, "高温告警");
			}
			else if(buf[15]&0x01){
				d->temperatureAlarm.value.str	= "低温告警";
				Set_Var_Status(&d->temperatureAlarm, Alarm, "低温告警");						
			}
			else{
				d->temperatureAlarm.value.str	= "无告警";
				Set_Var_Status(&d->temperatureAlarm, Normal, "无告警");
			}

			if(buf[16]&0x80){
				d->humidityAlarm.value.str		= "高湿告警";
				Set_Var_Status(&d->humidityAlarm, Alarm, "高湿告警");
			}
			else if(buf[16]&0x40){
				d->humidityAlarm.value.str		= "低湿告警";
				Set_Var_Status(&d->humidityAlarm, Alarm, "低湿告警");						
			}
			else{
				d->humidityAlarm.value.str		= "无告警";
				Set_Var_Status(&d->humidityAlarm, Normal, "无告警");
			}

			d->returnAirTemperatureSensorFailure.value.str			= buf[16]&0x20?"有告警":"无告警";
			Set_Var_Status(&d->returnAirTemperatureSensorFailure, 	  buf[16]&0x20?Alarm:Normal, NULL);				
			d->supplyAirTemperatureSensorFailure.value.str			= buf[16]&0x10?"有告警":"无告警";
			Set_Var_Status(&d->supplyAirTemperatureSensorFailure, 	  buf[16]&0x10?Alarm:Normal, NULL);				
			d->returnAirHumiditySensorFailure.value.str				= buf[16]&0x08?"有告警":"无告警";
			Set_Var_Status(&d->returnAirHumiditySensorFailure,		  buf[16]&0x08?Alarm:Normal, NULL);				
			d->outdoorTemperatureSensorFailure.value.str			= buf[16]&0x04?"有告警":"无告警";
			Set_Var_Status(&d->outdoorTemperatureSensorFailure,		  buf[16]&0x04?Alarm:Normal, NULL);				
			d->supplyAirTemperatureAlarm.value.str					= buf[16]&0x02?"有告警":"无告警";
			Set_Var_Status(&d->supplyAirTemperatureAlarm,			  buf[16]&0x02?Alarm:Normal, NULL);				
			
			d->humidifierOverload.value.str							= buf[17]&0x80?"有告警":"无告警";
			Set_Var_Status(&d->humidifierOverload, 					  buf[17]&0x80?Alarm:Normal, NULL);				
			d->humidifierLackOfWater.value.str						= buf[17]&0x40?"有告警":"无告警";
			Set_Var_Status(&d->humidifierLackOfWater, 				  buf[17]&0x40?Alarm:Normal, NULL);				
			d->noHumidifyingCurrent.value.str						= buf[17]&0x20?"有告警":"无告警";
			Set_Var_Status(&d->noHumidifyingCurrent,				  buf[17]&0x20?Alarm:Normal, NULL);				
			d->overflowAlarm.value.str								= buf[17]&0x02?"有告警":"无告警";
			Set_Var_Status(&d->overflowAlarm,						  buf[17]&0x02?Alarm:Normal, NULL);	
			d->userAlarm.value.str									= buf[17]&0x01?"有告警":"无告警";
			Set_Var_Status(&d->userAlarm,							  buf[17]&0x01?Alarm:Normal, NULL);	

			d->smokeAlarm.value.str									= buf[18]&0x80?"有告警":"无告警";
			Set_Var_Status(&d->smokeAlarm,							  buf[18]&0x80?Alarm:Normal, NULL);		

			if(buf[18]&0x04){
				d->compressor2PressureAlarm.value.str	= "高压告警";
				Set_Var_Status(&d->compressor2PressureAlarm, Alarm, "高压告警");
			}
			else if(buf[18]&0x02){
				d->compressor2PressureAlarm.value.str	= "低压告警";
				Set_Var_Status(&d->compressor2PressureAlarm, Alarm, "低压告警");						
			}
			else{
				d->compressor2PressureAlarm.value.str	= "无告警";
				Set_Var_Status(&d->compressor2PressureAlarm, Normal, "无告警");
			}

			d->waterFlowSwitchAlarm.value.str						= buf[18]&0x01?"有告警":"无告警";
			Set_Var_Status(&d->waterFlowSwitchAlarm,				  buf[18]&0x01?Alarm:Normal, NULL);	

			return 0;		
		}
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void jmkt_SDAxxx_Registration(void)
* 函数说明： 依米康精密空调驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void jmkt_SDAxxx_Registration(void)
{
	DeviceDriverRegistration(
		"jmkt",													//设备类型
		"精密空调",												//设备名称（导航栏默认显示名称）
		"依米康",												//设备品牌	
		"SDAxxx",												//设备型号
		"适用于SDA系列小型机房专用空调，协议为Modbus-RTU。",	//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		800														//设备响应超时时间		
	);
}

